from django.db import models
from django.contrib.auth.models import User
from public.models import CommonInfo
from django.db.models.signals import post_save
from django.dispatch import receiver
from rest_framework.authtoken.models import Token
from django.conf import settings


# @receiver() 表示当发送者(sender=settings.AUTH_USER_MODEL)对象的指定动作(post_save)发生时，提醒接收者（触发装饰的函数create_auth_token）
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
    if created:
        Token.objects.create(user=instance)


class App(CommonInfo):
    """应用"""
    jenkins_job = models.CharField(max_length=255, verbose_name="JENKINS JOB名称")
    git_url = models.CharField(max_length=512, verbose_name="Git地址")
    dir_build_file = models.CharField(max_length=512, verbose_name="编译目录")
    build_cmd = models.CharField(max_length=512, default="./", verbose_name="编译命令")
    is_restart_status = models.BooleanField(default=True, verbose_name="是否重启")
    package_name = models.CharField(max_length=128, blank=True, null=True, verbose_name="软件包名")
    zip_package_name = models.CharField(max_length=128, blank=True, null=True, verbose_name="压缩包名")
    op_log_no = models.IntegerField(blank=True, null=True, default=0, verbose_name="启停服务日志次数")
    manage_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="manage_user",
                                    blank=True, null=True, verbose_name="App管理员")
    script_url = models.CharField(max_length=128, blank=True, null=True, verbose_name="App脚本链接")

    def __str__(self):
        return self.name

    class Meta:
        db_table = "App"
        ordering = ('-add_date',)
